package com.android.booksreader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class WelcomeView extends SurfaceView {

	private Bitmap welcomePic;
	private Paint paint;
	private Canvas canvas;
	private Handler myHandler;
	private MyThread myThread;

	public WelcomeView(Context context, Handler myHandler) {
		super(context);
		this.myHandler = myHandler;
		welcomePic = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
		canvas = new Canvas(welcomePic);
		// 初始化画笔
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTextScaleX(20);
		getHolder().addCallback(new CallBack());
		// new thread
		myThread = new MyThread(myHandler);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawBitmap(welcomePic, 0, 0, paint);
	}

	class CallBack implements SurfaceHolder.Callback {

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			myThread.start();
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {

		}

	}

	class MyThread extends Thread {
		private Handler handler;

		public MyThread(Handler handler) {
			this.handler = handler;
		}
		
		@Override
		public void run() {
			try {
				// 欢迎界面展示两秒钟
				sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			handler.sendEmptyMessage(1);
		}

	}
}
